const formatDateYMDHM = (date) => `${date.getFullYear()}-${(date.getMonth() + 1)<10?'0'+(date.getMonth() + 1):date.getMonth() + 1}-${(date.getDate())<10?'0'+(date.getDate()):date.getDate()} ${(date.getHours())<10?'0'+(date.getHours()):date.getHours()}:${(date.getMinutes())<10?'0'+(date.getMinutes()):date.getMinutes()}`;
/** 
 * file转base64
*/
const blobToDataURL = (blob, cb) => {
    let reader = new FileReader();
    reader.onload = function (evt) {
        let base64 = evt.target.result;
        cb(base64);
    };
    reader.readAsDataURL(blob);
};

const formatName = (name) => {
    let newStr;
    if (name.length === 2) {
        newStr = name.substr(0, 1) + '*';
    } else if (name.length > 2) {
        let char = '';
        for (let i = 0, len = name.length - 2; i < len; i++) {
            char += '*';
        }
        newStr = name.substr(0, 1) + char + name.substr(-1, 1);
    } else {
        newStr = name;
    }
    return newStr;
};

const countDownFun =  (time) => {
    time--;//时间一秒秒的减
    let nowTime = new Date().getTime(); //现在时间
    if(nowTime<=time){
    //获取时间差
    let timediff=Math.round((time-nowTime)/1000);
    //获取还剩多少天
    let day=parseInt(timediff/3600/24);
    //获取还剩多少小时
    let hour=parseInt(timediff/3600%24);
    //获取还剩多少分钟
    let minute=parseInt(timediff/60%60);
    //获取还剩多少秒
    let second= timediff%60;
    day = day > 9 ? day : "0" + day;
    hour = hour > 9 ? hour : "0" + hour;
    minute = minute > 9 ? minute : "0" + minute;
    second = second > 9 ? second : "0" + second;
    if(day == 0) {
      return hour+":" + minute+ ":"+ second
    }
    return day+"天" + hour+":" + minute+ ":"+ second
    }else{
    return "00:00:00"
    }
    }


export default{
    formatDateYMDHM,
    blobToDataURL,
    formatName,
    countDownFun,
}